﻿#region Using

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System.Media;

#endregion

namespace ExtendedDialogBox
{
	
	#region Flags
	
	[Flags]
	public enum ExtendedDialogBoxButtons
	{ 
		Ok = 1, 
		Yes = 2,
		No = 4, 
		Cancel = 8 
	}
	
	#endregion
	
	internal static class function
	{
		#region Methods
		
		/// <summary>
		/// Esegue un suono wav tra i predefiniti di una MessageBox in base al tipo di icona
		/// </summary>
		/// <param name="iconType">Tipo di icona</param>
		public static void PlaySound(MessageBoxIcon iconType)
		{
			if (iconType == MessageBoxIcon.Question)
				SystemSounds.Question.Play();
			else if (iconType == MessageBoxIcon.Exclamation)
				SystemSounds.Exclamation.Play();
			else if (iconType == MessageBoxIcon.Error)
				SystemSounds.Hand.Play();
			else if (iconType == MessageBoxIcon.Warning)
				SystemSounds.Hand.Play();
			else if (iconType == MessageBoxIcon.Asterisk)
				SystemSounds.Asterisk.Play();
			else
				SystemSounds.Beep.Play();
		}
		
		/// <summary>
		/// Ottiene l'icona associata al tipo
		/// </summary>
		/// <param name="iconType">Tipo di icona</param>
		/// <returns>Icona</returns>
		public static Icon GetIcon(MessageBoxIcon iconType)
		{
			if (iconType == MessageBoxIcon.Question)
				return SystemIcons.Question;
			else if (iconType == MessageBoxIcon.Exclamation)
				return SystemIcons.Exclamation;
			else if (iconType == MessageBoxIcon.Error || iconType == MessageBoxIcon.Stop)
				return SystemIcons.Error;
			else if (iconType == MessageBoxIcon.Warning)
				return SystemIcons.Warning;
			else if (iconType == MessageBoxIcon.Asterisk)
				return SystemIcons.Asterisk;
			else
				return SystemIcons.Information;
		}
		
		/// <summary>
		/// Ottieni la Label
		/// </summary>
		/// <param name="text">Testo della Label</param>
		/// <param name="foreColor">Colore del testo</param>
		/// <param name="size">Dimensioni del form</param>
		/// <param name="font">Font del testo</param>
		/// <returns>Label</returns>
		public static Label GetLabel(string text, Nullable<Color> foreColor, Size size, Font font)
		{
			Label label = new Label();
			label.Text = text;
			label.BackColor = Color.Transparent;
			if (foreColor.HasValue)
				label.ForeColor = (Color)foreColor;
			label.AutoSize = false;
			label.TextAlign = ContentAlignment.MiddleCenter;
			label.Location = new Point(10, 10);
			label.Width = size.Width - 20;
			label.Height = function.GetLabelHeight(ref label, ref font, text);
			label.Anchor = AnchorStyles.Bottom;
			label.BorderStyle = BorderStyle.None;
			return label;
		}
		
		/// <summary>
		/// Ottiene l'altezza della Label in base alla lunghezza e/o alla presenza di ritorni a capo del testo
		/// </summary>
		/// <paramref="label">Label di riferimento</paramref>
		/// <paramref="font">Font di riferimento</paramref>
		/// <param name="text">Testo della Label</param>
		/// <returns>Altezza della Label (Height)</returns>
		public static int GetLabelHeight(ref Label label, ref Font font, string text)
		{
			int lines = 1;
			string[] esplicitLine = text.Split('\n');
			if (esplicitLine.Length > 1)
			{
				lines = esplicitLine.Length-1;
				for (int i = 0; i < esplicitLine.Length; i++)
				{
					int tot = (int)(TextRenderer.MeasureText(esplicitLine[i], font).Width / label.Width);
					if (tot > 0)
						lines += tot;
				}
			}
			else
			{
				int tot = (int)(TextRenderer.MeasureText(text, font).Width / label.Width);
				if (tot > 0)
					lines += tot;	
			}
			return (label.Height * lines);
		}
		
		/// <summary>
		/// Ottiene il nuovo Bottone con le impostazioni assegnate
		/// </summary>
		/// <param name="text">Testo del bottone</param>
		/// <param name="dialog">Tipo di dialog che deve restituire quando viene generat l'evento Click</param>
		/// <returns>Button</returns>
		public static Button GetButtonDetails(string text, FlatStyle style, DialogResult dialog)
		{
			Button button = new Button();
			button.Name = "btn_" + dialog.ToString();
			button.Text = text;
			button.FlatStyle = style;
			button.BackColor = Color.Transparent;
			button.Enabled = true;
			button.Visible = true;
			button.Cursor = Cursors.Hand;
			button.DialogResult = dialog;
			button.AutoSize = true;
			button.Anchor = AnchorStyles.Bottom;
			return button;
		}
		
		/// <summary>
		/// Imposta la posizione nel form dei Bottoni
		/// </summary>
		/// <paramref="buttons">Riferimento alla lista dei bottoni</paramref>
		/// <paramref="size">Dimensioni del form</paramref>
		public static void SetButtonsLocation(ref List<Button> buttons, Size size)
		{
			int count = buttons.Count;
			if (count == 1)
			{
				buttons[0].Location = new Point((size.Width / 2) - (buttons[0].Width / 2),
				                                (size.Height -20) - (buttons[0].Height / 2));
			}
			else if (count == 2)
			{
				buttons[0].Location = new Point((size.Width / 2) - buttons[0].Width - 5,
												(size.Height -20) - (buttons[0].Height / 2));
				buttons[1].Location = new Point((size.Width / 2) + 5,
				                                (size.Height -20) - (buttons[1].Height / 2));
			}
			else if (count == 3)
			{
				buttons[0].Location = new Point((size.Width / 2) - buttons[0].Width - (buttons[0].Width / 2) - 10,
				                                (size.Height -20) - (buttons[0].Height / 2));
				buttons[1].Location = new Point((size.Width / 2) - (buttons[1].Width / 2),
												(size.Height -20) - (buttons[1].Height / 2));
				buttons[2].Location = new Point((size.Width / 2) + (buttons[2].Width / 2) + 10,
				                                (size.Height -20) - (buttons[2].Height / 2));
			}
			else if (count == 4)
			{
				buttons[0].Location = new Point((size.Width / 2) - (buttons[0].Width * 2) - 15,
				                                 (size.Height -20) - (buttons[0].Height / 2));
				buttons[1].Location = new Point((size.Width / 2) - buttons[1].Width - 5,
												(size.Height -20) - (buttons[1].Height / 2));
				buttons[2].Location = new Point((size.Width / 2) + 5,
				                                (size.Height -20) - (buttons[2].Height / 2));
				buttons[3].Location = new Point((size.Width / 2) + buttons[3].Width + 15,
				                                 (size.Height -20) - (buttons[3].Height / 2));
			}
		}
		
		#endregion
	}
}
